<template>
	<view class="index_content_basic">
		<view class="index_content_basic_main">
			<u--form :model="ruleForm" :rules="rules" label-position="top" ref="ruleForm" label-width="100px"
				class="demo-ruleForm">
				<u-form-item label="技能名称" prop="skillName">
					<u-input v-model="ruleForm.skillName" placeholder="请输入技能名称"></u-input>
				</u-form-item>
				<u-form-item label="掌握时间" prop="masterTime">
					<!-- <TimeSelect :placeholders="'掌握时间'" :value="ruleForm.masterTime" :keys="'masterTime'" @mySelectclick="mySelectclick"></TimeSelect> -->
					<!-- <el-date-picker type="date" format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss"
						placeholder="请选择掌握时间" v-model="ruleForm.masterTime" style="width: 100%;">
					</el-date-picker> -->
					<!-- <el-date-picker clearable type="month" format="yyyy-MM" value-format="yyyy-MM" placeholder="请选择掌握时间"
						v-model="ruleForm.masterTime" style="width: 350px;">
					</el-date-picker> -->
					<u-input  v-model="ruleForm.masterTime" type="number" placeholder="请输入掌握时间">
						<template slot="suffix">
							月
						</template>
					</u-input>
				</u-form-item>
				<u-form-item label="熟练程度" prop="proficiencyLevel">
					<mySelect :dict="'proficiency_level'" :value="ruleForm.proficiencyLevel" :keys="'proficiencyLevel'"
						@mySelectclick="mySelectclick"></mySelect>
					<!-- <el-select v-model="ruleForm.proficiencyLevel" placeholder="请选择熟练程度" style="width: 100%;">
						<el-option v-for="item in proficiencyLevelOptions" :label="item.dictLabel"
							:value="item.dictValue">
						</el-option>
					</el-select> -->
				</u-form-item>
			</u--form>
		</view>
		<view class="position_footer flex aic jcsb">
			<u-button class="blue_btn" style="width: 100%;" type="primary" @click="submitForm('ruleForm')">保存
			</u-button>
			<view class="" style="width: 44%;margin-left: 16px;" v-if="type == 0">
				<u-button class="" type="error" @click="deleteFun()">
					删除
				</u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import mySelect from "../../../components/mySelect.vue"
	import TimeSelect from "../../../components/TimeSelect.vue"
	import {
		editResume
	} from "@/api/my.js"
	export default {
		components: {
			mySelect,
			TimeSelect
		},
		data() {
			return {
				type:0,
				proficiencyLevelOptions: [],
				ruleForm: {
					skillName:"",
					masterTime:"",
					proficiencyLevel:"",
				},
				rules: {
					skillName: [{
						required: true,
						message: '请输入技能名称',
						trigger: 'blur'
					}],
					masterTime: [{
						required: true,
						message: '请输入掌握时间',
						trigger: 'blur'
					},{
						type:"number",
						min: 1,
						required: true,
						message: '请输入数字格式',
						trigger: 'blur'
					}],
					proficiencyLevel: [{
						required: true,
						message: '请选择熟练程度',
						trigger: 'blur'
					}],
				}
			};
		},
		onReady() {
			this.$refs.ruleForm.setRules(this.rules);
		},
		onLoad(options) {
			// this.proficiencyLevelOptions = uni.getStorageSync("proficiency_level")
			this.datas = JSON.parse(options.data)
			this.type = options.type
			if (options.type == 0) { //修改
				this.indexValue = options.index
				this.info(this.datas, options.index)
			} else { //新增	
			}

		},
		methods: {
			mySelectclick(e) {
				console.log("下拉框组件回传:", e);
				this.ruleForm[e.key] = e.value
			},
			timeFun() {
				this.ruleForm.masterTime = this.ruleForm.masterTime.slice(0, 10)
			},
			timeFunEnd() {
				this.ruleForm.masterTime = this.ruleForm.masterTime + " 00:00:00"
			},
			info(data, index) {
				console.log("data:", data);
				this.ruleForm = data.skills[index]
				// this.timeFun()
			},
			deleteFun() {
				let obj = this.datas
				obj.skills.splice(this.indexValue, 1)
				editResume(obj).then(res => {
					uni.showToast({
						title: "已删除",
						icon: 'error'
					})
					if (this.type == 1) {
						uni.navigateBack("-1")
					}
				}).catch(res => {
					uni.showToast({
						title: res.msg,
						icon: 'error'
					})
				})
			},
			submitForm(formName) {
				this.$refs.ruleForm.validate().then(valid => {
					if (valid) {
						let obj = this.datas
						console.log("专业技能数据：", obj);
						// this.timeFunEnd()
						if (this.type == 0) {
							obj.skills[this.indexValue] = this.ruleForm
						} else {
							obj.skills.push(this.ruleForm)
						}

						editResume(obj).then(res => {
							uni.showToast({
								title: "保存成功",
								icon: 'success'
							})
							if (this.type == 1) {
								uni.navigateBack("-1")
							}
						}).catch(res => {
							uni.showToast({
								title: res.msg,
								icon: 'error'
							})
						})
					} else {
						this.$message({
							message: '请完善信息',
							type: 'error',
							offset: 0.1
						});
						console.log('error submit!!');
						return false;
					}
				});
			},

		}
	}
</script>

<style lang="scss" scoped>
	.blue_btn {
		height: 40px;
		background: #2F6CBC;
		border: 1px solid #2F6CBC;
	}
</style>
